#!/bin/bash
# F. Dang
# Run as a bash script with ./clean.sh and not sh clean.sh
# Clean the generated files from CMake and make

echo "Cleaning project..."

dir=`echo "$0" | sed 's,[^/]*$,,'`
test "x${dir}" = "x" && dir='.'

if test "x`cd "${dir}" 2>/dev/null && pwd`" != "x`pwd`"; then
    echo "This script must be executed directly from the source directory !"
    exit 1
fi

if [ -e "Makefile" ]; then
	make clean
	echo "make clean [OK]"
fi

declare -a target=(
	'bin/' 'CMakeFiles/' 'CMakeCache.txt' 'cmake_install.cmake' 
	'config.h' 'Makefile' 'doc/' 'gmon.out');

i=0
n=${#target[@]}	#Number of elements in target
while [ "$i" -lt "$n" ]
do
	if [ -e ${target[$i]} ]; then
		if [ -d ${target[$i]} ]; then	# Check wether it is a directory or not
			rm -rf ${target[$i]}
		else
			rm ${target[$i]}
		fi
		echo "Clean ${target[$i]}  [OK]"
	fi

	let "i = $i + 1"
	# or ((i++))
done


echo "Cleaning completed."

